#include "database.h"
#include "connection.h"
#include "dbalbum.h"
#include "dbgenre.h"
#include "dbartist.h"
#include <QSqlQuery>
#include <QString>
#include <QSqlError>
#include <QVariant>
#include <QSqlRecord>
#include <QDebug>

DataBase::DataBase()
{
}

void DataBase::getAll(Collection &collection)
{
    if(DbArtist::getAll(collection) && DbAlbum::getAll(collection)
            && DbGenre::getAll(collection))
    {
        Connection *con = Connection::getInstance();
        if(con->isConnected())
        {

            QString requete = "SELECT id, name, time, lyrics, score, num,"
                    " recovery, heart, location, album, artist, genre"
                    " FROM track";
            QSqlQuery query(requete);
            QSqlRecord rec = query.record();
            int idCol = rec.indexOf("id");
            int nameCol = rec.indexOf("name");
            int timeCol = rec.indexOf("time");
            int lyricsCol = rec.indexOf("lyrics");
            int scoreCol = rec.indexOf("score");
            int numCol = rec.indexOf("num");
            //            int recoveryCol = rec.indexOf("recovery");
            int heartCol = rec.indexOf("heart");
            int locationCol = rec.indexOf("location");
            int albumCol = rec.indexOf("album");
            int artistCol = rec.indexOf("artist");
            int genreCol = rec.indexOf("genre");
            while(query.next())
            {
                QTime time = QTime::fromString(query.value(timeCol).toString(),
                                               "hh:mm:ss");
                int idArt = query.value(artistCol).toInt();
                DtArtist *art = NULL;
                if(idArt != -1)
                {
                    art = collection.getArtistId(idArt);
                    /* if the artist isn't in the collection ze get bck from the
                 * database */
                    if(art == NULL)
                    {
                        /* Then we get it... */
                    }
                }
                int idAlb = query.value(albumCol).toInt();
                DtAlbum *alb = NULL;
                if(idAlb != -1)
                {
                    alb = collection.getAlbumId(idAlb);
                    if(art == NULL)
                    {

                    }
                }
                int idGen = query.value(genreCol).toInt();
                DtGenre * gen = NULL;
                if(idGen != -1)
                {
                    gen = collection.getGenreId(idAlb);
                    if(gen == NULL)
                    {

                    }
                }

                DtTrack *tra = new DtTrack(query.value(locationCol).toString(),
                                           query.value(nameCol).toString(),
                                           query.value(numCol).toInt(),
                                           query.value(idCol).toInt(),
                                           alb,
                                           art,
                                           gen,
                                           time,
                                           query.value(lyricsCol).toString(),
                                           query.value(scoreCol).toInt(),
                                           NULL, query.value(heartCol).toBool());
                collection.addTrack(tra, alb, art, gen);
            }
        }
    }
}
